// pages/index/date_list.js
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var cour_id = options.cour_id;
    var user_id = wx.getStorageSync('user_msg').id    
    console.log(options)
    var that=this;
    app.util.request({
      url: "entry/wxapp/Unit",
      'cachetime': '0',
      data:{
        cour_id: cour_id,
        user_id: user_id
      },
      success: function (res) {
        console.log(res)
        if(res.data.code==200){
          var course_name = res.data.list[0].course_name
          var name = res.data.list[0].name
          var title = course_name + name
          that.setData({
            date: res.data.list,
            course_name: course_name,
            name:name,
            none: res.data,
          })
        }else{
          that.setData({
            // date: res.data.list,
            none: res.data
          })
        }
      }
    })
  },

  /*点击进入作业列表 */
  task_list: function (e) {
    console.log(e)
    var that=this;
    console.log(that)
    var index = e.currentTarget.dataset.index;
    var unit_id = e.currentTarget.dataset.id;
    var state = e.currentTarget.dataset.state;
    // console.log(user_id)
    console.log(state)
    var subject = e.currentTarget.dataset.subject;
    var name = e.currentTarget.dataset.name;
    // console.log(subject)
    // console.log(name)
    var user_id = wx.getStorageSync('user_msg').id
    if (subject!=name){
      wx.showToast({
        title: '学级不符,不能进入答题!',
        icon: 'none',
        duration: 2000
      })      
    }else if(state == 1) {
      //没有批改
      if (index==0){
        wx: wx.navigateTo({
          url: 'task_list?unit_id=' + unit_id + "&user_id=" + user_id,
        })
      }else{
        var prev_num = index-1
        console.log(prev_num)
        var state = that.data.date[prev_num].zy_state
        if (state==2){
          wx: wx.navigateTo({
            url: 'task_list?unit_id=' + unit_id + "&user_id=" + user_id,
          })          
        }else{
          wx.showToast({
            title: '上一课节老师批改以后才能进入下个课节',
            icon: 'none',
            duration: 2000
          })          
        }
      }
    } else if (state == 2){
      //已经批改
      wx: wx.navigateTo({
        url: "../my/task?userid=" + user_id
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})